﻿using Microsoft.Extensions.DependencyInjection;
using Sx.Utils;
using System;
using System.Collections.Generic;
using System.Text;

namespace Sx.Web.Framework.Extensions
{
    public static class CacheExtensions
    {
        public static IServiceCollection AddCache(this IServiceCollection services)
        {
            string cacheType = ConfigHelper.GetString("Cache:Type");
            if (cacheType == "Redis")
            {
                return services.AddDistributedRedisCache(opt=> {
                    opt.InstanceName = ConfigHelper.GetString("Cache:Redis:InstanceName");
                    opt.Configuration = ConfigHelper.GetString("Cache:Redis:Configuration");
                });
            }
            else if (cacheType == "Memory")
            {
                return services.AddDistributedMemoryCache();
            }
            else
            {
                return services.AddDistributedNullCache();
            }
        }
    }
}
